## widget objects
import libtcod.libtcodpy as libtcod
import jplur.colors as colors
import jplur

hextorgb = colors.hextorgb

def sanity(width, height, x, y):
	if x >= 0 and x <= width:
		if y >= 0 and y <= height:
			return True

class percentage_bar:
	def __init__(self, libtcod, x = 0, y = 0, width = 10, height = 2, text="%bar", color="green", back_color="red"):
		self.libtcod = libtcod
		self.x = x
		self.y = y
		self.width = width
		self.height = height
		self.text = text
		self.color = color
		self.back_color = back_color

	def draw(self, curr=100, max=100):
		self.libtcod.console_set_background_color(0, eval("libtcod."+self.back_color))
		self.libtcod.console_set_foreground_color(0, eval("libtcod.black"))
		if curr == 0:
			curr = 1.0
		if max == 0:
			return

		perc = float(curr)/float(max)
		length = ""
		for i in range(self.width): length += " "
		libtcod.console_print_left(0, self.x, self.y, libtcod.BKGND_SET, length)
		perc = int( float(self.width)*perc )
		length = ""
		for i in range(perc): length += " "
		libtcod.console_set_background_color(0, eval("self.libtcod."+self.color))
		libtcod.console_print_left(0, self.x, self.y, libtcod.BKGND_SET, length)
		libtcod.console_print_left(0, self.x, self.y, libtcod.BKGND_NONE, self.text)

class textbox:
	def __init__(self, libtcod, console, x = 0, y = 0, width = 40, height = 10, border=False, border_color="white", back_color="dark_blue", color="white"):
		self.libtcod = libtcod
		self.console = console
		self.x = x
		self.y = y
		self.width = width
		self.height = height
		self.border = border
		self.border_color = border_color
		self.back_color = back_color
		self.color = color

		if x < 0: self.x = 0
		if y < 0: self.y = 0
		
		if width + x > self.console.width: self.width = self.console.width - x
		if height + y > self.console.height: self.height = self.console.height - y

	def draw(self, text=["A text box"], color="white"):

		libtcod.console_set_background_color(0, eval("libtcod."+self.back_color))
		
		for i in range(self.height):

			for j in range(self.width):
				
				if self.border and ( i in [0,self.height-1] ):
					libtcod.console_set_foreground_color(0, eval("libtcod."+self.border_color))
					libtcod.console_print_left(0, j+self.x, i+self.y, libtcod.BKGND_SET, self.border)
				elif self.border and ( j in [0,self.width-1] ):
					libtcod.console_set_foreground_color(0, eval("libtcod."+self.border_color))
					libtcod.console_print_left(0, j+self.x, i+self.y, libtcod.BKGND_SET, self.border)
				else:
					libtcod.console_set_foreground_color(0, eval("libtcod."+self.color))
					libtcod.console_print_left(0, j+self.x, i+self.y, libtcod.BKGND_SET, " ")
		
		i = 0
		libtcod.console_set_foreground_color(0, eval("libtcod."+self.color))
		for entry in text:
			if self.border:
				x = 1
				y = 1
			else:
				x = 0
				y = 0
			
			libtcod.console_print_left(0, x+self.x, i+y+self.y, libtcod.BKGND_SET, entry)
			i += 1

class messagebox:
	def __init__(self, libtcod, console, x = 0, y = 0, width = 40, height = 10, back_color="dark_blue", color="white"):
		self.libtcod = libtcod
		self.console = console
		self.x = x
		self.y = y
		self.width = width
		self.height = height
		self.back_color = back_color
		self.color = color

		self.lines = []

		if x < 0: self.x = 0
		if y < 0: self.y = 0
		
		if width + x > self.console.width: self.width = self.console.width - x
		if height + y > self.console.height: self.height = self.console.height - y

	def update(self, lines):
		if type(lines) == str:
			self.lines.append(lines)
		elif type(lines) == list:
			self.lines += lines

		#knock off extra lines?
			#or save for future use
	def draw(self):

		libtcod.console_set_background_color(0, eval("libtcod."+self.back_color))
		
		for i in range(self.height):
			for j in range(self.width):
				libtcod.console_print_left(0, j+self.x, i+self.y, libtcod.BKGND_SET, " ")
		
		i = 0
		libtcod.console_set_foreground_color(0, eval("libtcod."+self.color))
		if len(self.lines) > self.height:
			printable = self.lines[-self.height:]
		else:
			printable = self.lines

		
		for line in printable:
			mute = 0
			#need sanity check for width, lines
			x = 0
			y = 0
			#split line, do colors
			
			#don't draw more than widget box!
			#pull chars, if color, print and set new color
			tt = 0
			ii = 0
			for character in line:
				if character == "#":
					try:
						word = line[ii:ii+8]
						mute = 8
						
						rgb = libtcod.Color(hextorgb(word[2:])[0], hextorgb(word[2:])[1], hextorgb(word[2:])[2])
						if word[1:2] == ':':
							libtcod.console_set_foreground_color(0, rgb)
						elif word[1:2] == ';':
							libtcod.console_set_background_color(0, rgb)
					except:
						pass
				if ii < self.width and mute <= 0:
					libtcod.console_print_left(0, x+self.x+tt, i+y+self.y, libtcod.BKGND_SET, character)
					tt += 1
				ii += 1
				
				mute -= 1
			i += 1
			libtcod.console_set_foreground_color(0, eval("libtcod."+self.color))
			libtcod.console_set_background_color(0, eval("libtcod."+self.back_color))
			



class mapbox:
	def __init__(self, libtcod, console, x = 0, y = 0, width = 40, height = 40, back_color="dark_blue", color="white"):
		self.libtcod = libtcod
		self.console = console
		self.x = x
		self.y = y
		self.width = width
		self.height = height
		self.back_color = back_color
		self.color = color
		self.displayed = 0
		

		self.lines = []
		for i in range(height):
			self.lines.append([])

	def update(self, data):
		jplur.map.update(data)
			
	def draw(self):
		
		if "map" in jplur.__dict__:
			self.displayed = jplur.map.total_player_view() #this can be updated sparsely
			libtcod.console_print_left(0, self.x+1, self.y-1, libtcod.BKGND_SET, "sector: "+str(jplur.map.key(jplur.map.player_location))+ " location: "+ str(jplur.map.player_location))
			y = 0
			s = 0
			placement = [ [0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2] ]

			last = -666
			tiles = colors.tiles()
			
			for sector in self.displayed:
				
				for line in sector:
					x = 0
					
					for tile in line:
						try:
							tiles[tile]
							go = 1
						except:
							print "mapbox error: no tile reference in color dict"
							character = "?"
						
							
						if go == 1:
							if tile != last: #new pattern
								rgb = hextorgb(tiles[tile][2])
								foreground = libtcod.Color(rgb[0], rgb[1], rgb[2])
								rgb = hextorgb(tiles[tile][1])
								background = libtcod.Color(rgb[0], rgb[1], rgb[2])
								
								libtcod.console_set_foreground_color(0, foreground )
								libtcod.console_set_background_color(0, background )
								character = tiles[tile][0]
							last = tile
						if self.x+x >= self.x and self.x+x <= self.x+self.width:
							if self.y+y >= self.y and self.y+y <= self.y+self.height:
							
								sx = placement[s][0] * 8
								sy = placement[s][1] * 8
								 
								libtcod.console_print_left(0, self.x+x+sx, self.y+y+sy, libtcod.BKGND_SET, character)
						
						x += 1
					y += 1
				s += 1
				y = 0
				
			#for entity, cull, then adjust it's location to the display and print
			cull_list = []
			
			for key in jplur.map.entities: #symbol color location
				entity = jplur.map.entities[key]
				loc = entity[2]
				bounds = jplur.map.display_bounds()

				if loc[0] == jplur.map.player_location[0] and loc[2] >= bounds[0] and loc[2] < bounds[2] and loc[1] >= bounds[1] and loc[1] < bounds[3]:
					# display it
					rgb = hextorgb(entity[1])
					foreground = libtcod.Color(rgb[0], rgb[1], rgb[2])
					background = libtcod.Color(0, 0, 0)
					libtcod.console_set_foreground_color(0, foreground )
					if jplur.highlite != -1:
						if jplur.highlite == key:
							background = libtcod.Color(236, 201, 36)
					
					libtcod.console_set_background_color(0, background )
					libtcod.console_print_left(0, loc[2]-bounds[0]+1, loc[1]-bounds[1]+1, libtcod.BKGND_SET, entity[0])
				else:
					cull_list.append(key)
			for unwanted in cull_list:

				print "removing entity:",jplur.map.entities.pop(unwanted)
				
			
